import gdb
import re

Target = "a.out"
Args = ""
CrashFilename = "crashFile.txt"
OutputFile = "crashLine.txt"


with open(CrashFilename, 'r') as f:
    crashFiles = f.readlines()

gdb.execute("file "+Target)
gdb.execute("set height unlimited")

output = open(OutputFile,'w')

stack2fileMap = {}
stackSet = set()

for file in crashFiles:
    gdb.execute("b main")
    gdb.execute("r "+Args.format())
    stackInfo = gdb.execute("bt", to_string=True)
    
    # 正则:去掉参数和地址
    address = re.compile('0x[0-9a-f]+')
    param = re.compile('\(.+\)')
    s = re.sub(address, '', stackInfo)
    s = re.sub(param, '', s)
    
    # 使用set降重
    if s not in stackSet:
        stackSet.add(s)
        stack2fileMap[s] = file
        output.write(s)
    else:
        output.write(file+' == '+stack2fileMap[s])
    
    output.write('-'*30)
    
output.close()
         
    